const app = getApp();
const util = require('../../../../utils/util.js');
const api = require('../../../../config/api.js');
Page({
  data: {
    auxiliary: { "current": 0,"showPopup":false},
    playAudio: false,//音频播放
    journeyData: { 
      "imageList": [{ "ip": '', "path": "https://image.xingqubaike.com:8443/minicard/upload/diary/2019/07/15/af77d4ef-40b4-4850-bba3-c47b35673d3a.jpg",
        "tags": [{
          "type": 0, "addr": "天利中央花园五栋", "latitude": 22.992, "longitude": 113.751, "address": "南城西平", "top": 230, "left": 130, "rotate": 180
        }, { "type": 1, "title": "我的圈子", "circleid": 787, "top": 80, "left": 150, "rotate": 0 }, { "type": 4, "linkType": 1, "linkName": "陶尔米纳", "linkIp":"","linkSrc": "https://mp.weixin.qq.com/s/pvgsdZkmsfqBucBAFDH3xg", "top": 280, "left": 156, "rotate": 180,}, { "type": 5, "text": "哈哈哈哈哈哈哈", "top": 200, "left": 150, "rotate": 0}],
        "decals": [{ "src": "https://image.xingqubaike.com:8443/minicard/ucenter/personhome/homesetup/tiezhizhu.png", "top": 90, "left": 285 }, { "src": "https://image.xingqubaike.com:8443/minicard/ucenter/personhome/homesetup/tiezhizhu.png", "top": 320, "left": 25}],
        "luyin": {
          "ip": "https://image.xingqubaike.com:8443", "url": "/audio/diary/2019/07/26/062daa92-f507-440f-82c2-05672412dfe7.mp3", "name": "A____丶Lin沣的音频", "startDate": "00:00","totalDate":"00:04"}
        }, { "ip": '', "path": "https://image.xingqubaike.com:8443/minicard/upload/diary/2019/07/15/af77d4ef-40b4-4850-bba3-c47b35673d3a.jpg" }],
      "userData": { "avatarip": "", "avatarpath": "https://image.xingqubaike.com:8443/minicard/upload/avatar/cb8bd659-f6e0-4d55-b921-81a060865677.png", "avatarurl": "", "gender": 1,"nickname":"取个什么名字好呢"},
      "intro": "星期六我像疯狂燃烧的野火Guess I won't be coming to church on Sunday星期日我将不会去教堂I'll be waiting for love, waiting for love我会等待爱的到来，等待爱To cover on让我痊愈",
      "addr": "天利中央花园五栋",
      "addrLatitude": 22.992,
      "addrLongitude": 113.751,
      "address": "南城西平"
    },
  },
  onLoad: function (options) {
    var _this = this;
    _this.setData({
      logininfo: app.globalData.logininfo,
      openid: app.globalData.logininfo.openid
    });
    //判断是不是用户本人访问
    if (options.openid != null && options.openid != '') {
      _this.setData({
        getopenid: options.openid
      })
    } else {
      _this.setData({
        getopenid: app.globalData.logininfo.openid
      })
    }
  },
  /**
   * 滑动图片
   */
  selectCurrent:function(e){
    var _this = this
    _this.setData({
      ["auxiliary.current"]: e.detail.current,
    })
  },
  //开启音频播放
  showPlayAudio: function (e) {
    var _this = this
    var backgroundAudioManager = wx.getBackgroundAudioManager();//获取全局唯一的背景音频管理器
    var playAudio = _this.data.playAudio
    var index = e.currentTarget.dataset.index
    console.log(e)
    if (playAudio) {
      backgroundAudioManager.pause();//暂停播放音乐
      wx.stopBackgroundAudio();//暂停正在播放音乐
      _this.setData({
        playAudio: false
      })
    } else {
      wx.stopBackgroundAudio();//暂停正在播放音乐
      backgroundAudioManager.startTime = 0;
      backgroundAudioManager.title = _this.data.journeyData.imageList[index].luyin.name
      backgroundAudioManager.src = _this.data.journeyData.imageList[index].luyin.ip + _this.data.journeyData.imageList[index].luyin.url
      backgroundAudioManager.play();
      _this.setData({
        playAudio: true
      })
    }
  },
  /**
   * 使用微信内置地图查看位置
   */
  wxGetLocation: function (e) {
    var name = e.currentTarget.dataset.name;
    var address = e.currentTarget.dataset.address;
    var latitude = e.currentTarget.dataset.latitude;
    var longitude = e.currentTarget.dataset.longitude;
    wx.openLocation({
      latitude: latitude,//要去的纬度-地址
      longitude: longitude,//要去的经度-地址
      name: name,
      address: address
    })
  },
  /**
   * 进入圈子详情页面
   */
  navigateToCircleInfo: function (e) {
    var _this = this;
    var circleid = e.currentTarget.dataset.circleid;
    var openid = app.globalData.logininfo.openid;
    var getopenid = _this.data.getopenid;
    var data = {};
    data.circleid = circleid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        _this.userIsCircleJoinQuery(api.Home_UserIsCircleJoinApiUrl, data);
      })
    } else {
      _this.userIsCircleJoinQuery(api.Home_UserIsCircleJoinUrl, data);
    }
  },
  /**
   * 校验用户是否为已加入的圈子：请求
   */
  userIsCircleJoinQuery: function (url, data) {
    var _this = this;
    util.post(url, data).then(function (res) {
      var userIsCircleJoin = false;
      if (res.code == 200) {
        userIsCircleJoin = res.result.userIsCircleJoin;
      }
      console.log(userIsCircleJoin)
      if (userIsCircleJoin) {
        // 已加入
        wx.navigateTo({
          url: '/ucenter/pages/ucenter/personcenter/editdetails/circleinteraction/circleinteraction?circleid=' + data.circleid,
        })
      } else {
        // 未加入
        wx.navigateTo({
          url: '/ucenter/pages/ucenter/personcenter/editdetails/circledetails/circledetails?circleid=' + data.circleid,
        })
      }
    });
  },
  //看公众号文章
  navigateToWebView: function (e) {
    var ip = e.currentTarget.dataset.ip;
    var text = e.currentTarget.dataset.text;
    var type = e.currentTarget.dataset.type;
    if (type == 1) {
      wx.navigateTo({
        url: '/pages/home/webView/webView?src=' + text,
      })
    } else {
      wx.downloadFile({
        url: ip + text,
        success: function (res) {
          var filePath = res.tempFilePath;
          wx.openDocument({
            filePath: filePath,
            success: function (res) {
            }
          })
        }
      })
    }
  },
  //打开选择上传路径面板
  openPopup: function () {
    var _this = this
    _this.setData({
      ["auxiliary.showPopup"]: true,
    })
  },
  /**
   * 关闭面板
   */
  closeMC: function () {
    var _this = this
    _this.setData({
      ["auxiliary.showPopup"]: false,
    })
  },
})